package com.youlai.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.common.enums.StatusEnum;
import com.youlai.system.common.exception.BusinessException;
import com.youlai.system.common.result.ResultCode;
import com.youlai.system.model.entity.Product;
import com.youlai.system.model.entity.ProductCategory;
import com.youlai.system.model.form.CategoryForm;
import com.youlai.system.model.query.CategoryPageQuery;
import com.youlai.system.model.vo.CategoryPageVo;
import com.youlai.system.model.vo.CategoryTreeVo;
import com.youlai.system.service.ProductCategoryService;
import com.youlai.system.mapper.ProductCategoryMapper;
import com.youlai.system.service.ProductService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author zzps
* @description 针对表【t_product_category(商品种类表)】的数据库操作Service实现
* @createDate 2024-03-11 10:03:59
*/
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory>
    implements ProductCategoryService{

    @Resource
    private ProductService productService;

    @Override
    public IPage<CategoryPageVo> getCategoryPageList( CategoryPageQuery categoryPageQuery) {
        // 封装分页参数
        Page<CategoryPageVo> page = new Page<>(categoryPageQuery.getPageNum(), categoryPageQuery.getPageSize());
        // 查询数据
        return baseMapper.getCategoryPageList(page, categoryPageQuery);
    }

    @Override
    public boolean addCategory(CategoryForm categoryForm) {
        // 根据父级分类和分类名称查询是否存在
        boolean exists = lambdaQuery()
                .eq(ProductCategory::getParentId, categoryForm.getParentId())
                .eq(ProductCategory::getName, categoryForm.getName()).exists();
        if (exists) {
            throw new BusinessException(ResultCode.EXISTED);
        }
        // 数据转换
        ProductCategory category = new ProductCategory();
        BeanUtils.copyProperties(categoryForm, category);
        return save(category);
    }

    @Override
    public boolean updateCategory(CategoryForm categoryForm) {
        // 数据转换
        ProductCategory category = new ProductCategory();
        BeanUtils.copyProperties(categoryForm, category);
        return updateById(category);
    }

    @Override
    public boolean updateCategoryStatus(Long id, Integer status) {
        // 判断status是否合法
        if (!Objects.equals(status, StatusEnum.ENABLE.getValue()) && !Objects.equals(status, StatusEnum.DISABLE.getValue())) {
            throw new BusinessException(ResultCode.PARAM_VALID_ERROR);
        }
        // 更新状态
        return lambdaUpdate()
                .set(ProductCategory::getStatus, status)
                .eq(ProductCategory::getId, id)
                .update();
    }

    @Override
    public List<CategoryTreeVo> getCategoryTree() {
        return this.baseMapper.getCategoryTree();
    }

    @Override
    public List<CategoryPageVo> getCategoryByLevel(Integer level) {
        return this.baseMapper.getCategoryByLevel(level);
    }

    @Override
    public boolean removeCategoryById(Long id) {
        // 判断当前分类下是否有子分类
        boolean existsChild = lambdaQuery().eq(ProductCategory::getParentId, id).exists();
        if (existsChild) {
            throw new BusinessException(ResultCode.EXISTED_CHILD_CATEGORY);
        }
        // 判断当前分类是否关联商品
        boolean existsProduct = productService.lambdaQuery().eq(Product::getCategoryId, id).exists();
        if (existsProduct) {
            throw new BusinessException(ResultCode.EXISTED_ASSOCIATE_PRODUCT);
        }
        // 可以删除分类信息
        return removeById(id);
    }
}




